<?php
class ManagementController extends AppController{
	function index(){
		$this->loadModel('Screen');
		$dataScreen = $this->Screen->find('all', array(
			'conditions' => array('delete_flag' => 0)
		));
//		debug($dataScreen);die();
		$this->set(compact('dataScreen'));
		$this->set('title_for_content', 'Screen Managent');
		$this->set('active_main_menu_id', 1);
		$this->set('active_sub_menu_id', 0);
	}
	/**
	 * add man hinh test
	 * * Enter description here ...
	 * @param unknown_type $id
	 * @throws NotFoundException
	 */
	function add_screen(){
		if ($this->request->is('post')){
			$this->loadModel('Screen');
			$dataScreen = $this->request->data['Screen'];
			if ($this->__check_data_input($dataScreen['name'])){
				$this->Session->setFlash('Screen existing!');
				$this->redirect(array('controller' => 'management', 'action' => 'index'));
			}else {
				$dataSource = $this->Screen->getDataSource();
				$dataSource->begin();
				$this->Screen->create();
				if ($this->Screen->save($dataScreen)){
					$dataSource->commit();
					$this->Session->setFlash('Add successfully!', 'success');
					$this->redirect(array('controller' => 'management', 'action' => 'index'));
				} else {
					$dataSource->rollback();
				}
				
				$this->Session->setFlash('Add failed !!!');				
			}
		}
	}
	/**
	 * edit man hinh test
	 * Enter description here ...
	 * @param unknown_type $id
	 * @throws NotFoundException
	 */
	function edit_screen($idScreen = null){
		$this->layout = 'ajax';
		$this->loadModel('Screen');
		if (!$idScreen){
			throw new NotFoundException(__('Invalid Screen'));
		}
		$dataScreenInfo = $this->Screen->find('first', array(
			'conditions' => array(
				'Screen.id' => $idScreen
			)
		));
		
		if ($this->request->is('post')){
			$dataScreen = $this->request->data['Screen'];
			if ($dataScreen){
				if ($this->__check_data_input($dataScreen['name'], $idScreen)){
					$this->Session->setFlash('Screen existing!');
					$this->redirect(array('controller' => 'management', 'action' => 'index'));
				}else {
					$dataSource = $this->Screen->getDataSource();
					$dataSource->begin();
					$this->Screen->id = $idScreen;
					if ($this->Screen->save($dataScreen)){
						$dataSource->commit();
						$this->Session->setFlash('Edit successfully!', 'success');
						$this->redirect(array('controller' => 'management', 'action' => 'index'));
					} else {
						$dataSource->rollback();
					}
					$this->Session->setFlash('Edit failed!');	
				}
			}
		}
		$this->set('data', $dataScreenInfo);
	}
	/**
	 * delete
	 */
	function delete($id = null) {
		$this->loadModel('Screen');
		if (!id){
			$this->redirect($this->referer());
		}else{
			$deleteInfo = $this->Screen->updateAll(
				array('Screen.delete_flag' => 1),
				array('Screen.id' => $id)
			);
			if ($deleteInfo){
				$this->Session->setFlash('Delete successfully', 'success');
			}else{
				$this->Session->setFlash('Delete failed!');
			}
			$this->redirect($this->referer());
		}
	}
	/**
	 * delete all
	 */
	function delete_all() {
		if ($this->request->is('post')){
			$this->loadModel('Screen');
			$deleteInfo = $this->request->data;
//			debug($deleteInfo);die();
			if (!empty($deleteInfo)){
				$deleteIds = array();              
		        foreach($this->data['form'] as $id => $value){
		            if($value){
		              $deleteIds[] = $id;
		            }
		        }
//	        debug($deleteIds);die;
				if ($deleteIds){
					$this->Screen->updateAll(
						array('Screen.delete_flag' => 1),
						array('Screen.id' => $deleteIds)
					);
					$this->Session->setFlash('Delete Successfully!', 'success');
				}
			}
			$this->redirect($this->referer());
		}
	}
	
	
	/**
	 * check thong tin truoc khi edit, add Screen
	 */
	function __check_data_input($name = null, $id = null){
		$this->loadModel('Screen');
		if ($id){
			$check = $this->Screen->find('first', array(
						'conditions' => array(
							'Screen.name' => $name,
							'Screen.id !=' => $id
						)
				)
			);
		}else{
			$check = $this->Screen->find('first', array(
						'conditions' => array(
							'Screen.name' => $name,
						)
				)
			);
		}
		if (!$check){return false;}
		return true;
	}
	
	
	
	/**
	 * create test case theo man hinh da chon
	 */
	function create_test_case($id = null){
		if (!$id){
			$this->redirect(array('controller' => 'management', 'action' => 'index'));
		}
		$this->loadModel('Screen');
		$this->loadModel('Area');
		$idScreen = $id;
		$nameScreen = $this->Screen->read(null, $id);
		$areaInfo = $this->Area->find('all', array(
			'conditions' => array(
				'Area.screen_id' => $idScreen,
				'Area.delete_flag' => 0,
			)
		));
		if ($this->request->is('post')){
			$dataAreaInfo = $this->request->data['Area'];
			$dataResult = $this->Area->find('first', array(
					'conditions' => array(
						'Area.name' => $dataAreaInfo['name'],
						'Area.screen_id' => $idScreen
						
					)
				)
			);

			if ($dataResult){
				$this->Session->setFlash('Area existing');
			}else{
				if ($this->Area->save($dataAreaInfo)){
					$this->Session->setFlash('Add Area successfully!', 'success');
					$this->redirect(array('controller' => 'management', 'action' => 'create_test_case', $idScreen));
				}else{
					$this->Session->setFlash('Add Area failed!');
				}
			}
		}
//		debug($areaInfo);die;
//		debug($id);die();
		$this->set(compact('id', 'areaInfo', 'arrResult'));
		$this->set('title_for_content', 'Screen:'.$nameScreen['Screen']['name']);
		$this->set('active_main_menu_id', 1);
		$this->set('active_sub_menu_id', 0);
	}
	
	
	function items($idItem = null, $idScreen = null){
		$this->layout = 'ajax';
		$this->loadModel('Area');
		$dataItemInfo = $this->Area->find('all', array(
			'conditions' => array(
				'Area.screen_id' => $idScreen
			),
		));
		if (!$idItem){
			throw new NotFoundException(__('Invalid Items'));
		}
//		debug($dataItemInfo);die;
		$this->set(compact('idItem', 'idScreen', 'dataItemInfo')); 
	}
	
	/**
	 * hàm add mới cho mọi items (label, text, button, .....)
	 */
	function add_items(){
	if ($this->request->is('post')){
			$dataResultInfo = $this->request->data;
			// Item them la Label.............
			if (isset($dataResultInfo['Label'])){
				$this->loadModel('Label');
				$model = "Label";
				$this->__add_items($dataResultInfo, $model, $dataResultInfo['Label']['screen_id'] );
			}
			// Item them la Text..............
			if (isset($dataResultInfo['Text'])){
				$this->loadModel('Text');
				$model = "Text";
				$this->__add_items($dataResultInfo, $model, $dataResultInfo['Text']['screen_id']);
			}
			// Item them la checkbox..............
			if (isset($dataResultInfo['Checkbox'])){
				$this->loadModel('Checkbox');
				$model = "Checkbox";
				$this->__add_items($dataResultInfo, $model, $dataResultInfo['Checkbox']['screen_id']);
			}
			// Item them la link..............
			if (isset($dataResultInfo['Link'])){
				$this->loadModel('Link');
				$model = "Link";
				$this->__add_items($dataResultInfo, $model, $dataResultInfo['Link']['screen_id']);
			}
			// Item them la button..............
			if (isset($dataResultInfo['Button'])){
				$this->loadModel('Button');
				$model = "Button";
				$this->__add_items($dataResultInfo, $model, $dataResultInfo['Button']['screen_id']);
			}
			// Item them la radio..............
			if (isset($dataResultInfo['Radio'])){
				$this->loadModel('Radio');
				$model = "Radio";
				$this->__add_items($dataResultInfo, $model, $dataResultInfo['Radio']['screen_id']);
			}
		}
	}
	/**
	 * hàm dùng chung cho mọi Items khi thêm
	 * Enter description here ...
	 * @param unknown_type $data
	 * @param unknown_type $model
	 */
	function __add_items($data = null, $model = null, $idScreen = null){
		$this->loadModel($model);
		$result = $this->$model->find('first', array(
				'conditions' => array(
					'screen_id' => $idScreen,
					'area_id' => $data[$model]['area_id'],
					'item_name' => $data[$model]['item_name']
				)
		));
		if ($result){
			$this->Session->setFlash('Items existing!');
			$this->redirect(array('controller' => 'management', 'action' => 'create_test_case', $idScreen));
		}else{
			if ($this->$model->save($data)){
				$this->Session->setFlash('Add successfully!', 'success');
				$this->redirect(array('controller' => 'management', 'action' => 'create_test_case', $idScreen));
			}else{
				$this->Session->setFlash('Add failed!!');
				$this->redirect(array('controller' => 'management', 'action' => 'create_test_case', $idScreen));
			}
		}
		
	}
	/**
	 * delete and undelete Items 
	 */
	function delete_items($model = null, $id = null){
		if (!$model || !$id){
			$this->redirect($this->referer());
		}
		$this->loadModel($model);
		$deleteInfo = $this->$model->updateAll(
			array('delete_flag' => 1),
			array('id' => $id)
		);
		if ($deleteInfo){
			$this->Session->setFlash('Delete Successfully!', 'success');
			$this->redirect($this->referer());
		}else{
			$this->Session->setFlash('Delete failed!');
			$this->redirect($this->referer());
		}
		$this->redirect($this->referer());
		
	}
	/**
	 * Item show
	 */
	function item_show($model = null, $itemName = null){
		$this->layout = 'ajax';
		$this->loadModel($model);
		$dataItemInfo = $this->$model->find('first', array(
			'conditions' => array(
				'item_name' => $itemName
			),
		));
		if (!$itemName){
			throw new NotFoundException(__('Invalid Items'));
		}
//		debug($dataItemInfo);die;
		$this->set(compact('dataItemInfo', 'model', 'itemName')); 
	}
}




















